#!/usr/bin/env bash
#=======Header=======================================================|
#Slackjeff
#Download direct videos on xvideos
#
#VERSION 0.1beta
#====================================================================|

#========Tests
# Have deps?
#========Someone variables
# For current diretory
CDir="$(dirname $(readlink -f $0))"
#====================================================================|

# Resove the problematics for execution in othes directories
cd "$CDir"

for dep in "wget" "curl" "ffplay"; do
   type "$dep" 1>/dev/null 2>&1 || { echo "U NEED A \"$dep\" for continue."; exit 137 ;}
done

#========FUNC
_info() # More informations
{
cat <<END
Xshell - Download Direct videos ON xvideos

USAGE:
Xshell "link"

PARAMS:
   -h, --help
      Show this Help

   -d, --download
      Download a video
END
}

_push() # Push a video
{
   video="html5player.setVideoUrlHigh"
   # Find String
   get_url_video=$(curl -s "${1}" | grep "$video" | sed "s/html5pla.*('//; s/');//; s/ //g")
   # Get a Title video
   get_title=$(echo "$1" | sed "s/.*\///")
   title="${get_title:=porn_video${RANDOM}}" # Title null?
   echo "---------> WAIT! Download a ${title}"
   wget -q --show-progress -O "${title}.mp4" ${get_url_video}   # Download a video
	 cat <<- END
		RUN THE U VIDEO
		----------------
		← | Return frame
		→ | Next frame
		0 | Plus volume
		9 | Less volume
		q | Quit
	END
	 ffplay -loglevel -8 "${title}.mp4"
	 if [ "$?" != "0" ]; then
					 echo "This shit of code, fucker"
	 fi
}

#=====MAIN
case $1 in
   -d|--download)
       shift
       #Null? goto info
       [ $1 ] && _push "$@" || _info ;;
   *)  _info                         ;;
esac
